<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class LoginCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {

        $boot=auth()->check();
        if (!$boot){
            return  redirect(route('login'))->withErrors('未登录，请先去登录吆亲！！！！');
        }
        $auths = session('admin.auths');
        if ($auths !== true) {
            $currentRouteName = $request->route()->getName();

            $auths = array_filter(array_merge($auths, config('rbac.allow_route')));
//            dd($auths);
            $bool = in_array($currentRouteName, $auths);
            if (!$bool) {
                return "您没有对应的权限，请联系超级管理员分配您的权限吧！！！！";
            }
        }

        return $next($request);
    }

}
